/*
 *
 *  This file is part of Cfu
 *
 *  Cfu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Cfu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Cfu. If not, see <http://www.gnu.org/licenses/>.
 *
 *  mainwindow.cc - william le ferrand (william@corefarm.com) 29/04/2010 15:30
 *
 */


// STL
#include <stdio.h>
#include <iostream>

// QT
#include <QApplication>
#include <QDesktopWidget>
#include <QCloseEvent>
#include <QFileDialog>

// CFU
#include <mainwindow.hpp>
#include <ui_mainwindow.h>

mainwindow_t::mainwindow_t(QAction *submitJobAction) : 
  QMainWindow(),
  _ui(new Ui::MainWindow),
  _submitJobAction(submitJobAction) 
{
  _ui->setupUi(this);
  _ui->logo->setPixmap(QPixmap(":/images/logo"));
  _ui->format->addItem("targa (.tga)");
  _ui->centralwidget->setWindowIcon (QIcon(":/images/cf.ico")); 
  _ui->format->addItem("exr");
  _refresh ();
}

mainwindow_t::~mainwindow_t()
{
  delete _ui; 
}

void mainwindow_t::setEnabled (bool enable) 
{
  _ui->buttonLaunchJob->setEnabled (enable); 
}

void mainwindow_t::_refresh()
{
  //  _ui->buttonLaunchJob->setIcon(_launchJobAction->icon());
  _ui->buttonLaunchJob->setText(_submitJobAction->text());
  setEnabled (false); 
  updateStatus (QString ("Please select a XML file to render on the Corefarm")); 
  
  
}

void mainwindow_t::_onSelectXML()
{
  _file = QFileDialog::getOpenFileName(this, 
				       tr("Select XML file"), 
				       "", 
				       tr("Yafaray XML file (*.xml)"));
  if (_file.length () > 0) 
    {
      QString msg; 
      msg.append ("You have selected file ") ; 
      msg.append (_file); 
      msg.append (", now select the power you want and then launch the job"); 
      setEnabled (true); 
      
      updateStatus (msg); 
    }
}

void mainwindow_t::_onSubmit()
{
  std::cout << "@@ Launch button clicked\n"; 
  _submitJobAction->trigger();
  return; 
}

void mainwindow_t::closeEvent(QCloseEvent *e)
{
  emit visibleChanged(false);
  e->accept();
  _refresh (); 
  std::cout << "@@@ Close event detected\n"; 
}


void mainwindow_t::updateStatus(QString msg) 
{
   _ui->status->setText(msg);
   QApplication::processEvents();
}


QString mainwindow_t::getFile () 
{
  return _file; 
}

int mainwindow_t::getGhz () 
{
  return _ui->ghz->value(); 
}

int mainwindow_t::getIsExr ()
{
  return (_ui->format->currentIndex ()); 
}

/*

<connections>
  <connection>
   <sender>selectFileButton</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>_onSelectXML()</slot>

  </connection>
 </connections>

 <slots>
  <slot>_onSelectXML()</slot>
 </slots>

*/
